package com.zy.experience_system.controller;

import com.zy.experience_system.entity.StuInformation;
import com.zy.experience_system.entity.TeachInformation;
import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.TeachForm;
import com.zy.experience_system.service.TeachService;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeachController extends BaseController {

    @Autowired
    private TeachService teachService;

    @PostMapping("/createTeach")
    public ResultVO createTeach(@Validated @RequestBody TeachForm teachForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                teachService.save(teachForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error(500, "创建失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PutMapping("/update")
    public ResultVO update(@Validated @RequestBody TeachForm teachForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                teachService.save(teachForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error(500, "修改失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/all")
    public ResultVO allStudent() {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<TeachInformation> list = teachService.findAll();
                List<Map<String, Object>> mapList = new ArrayList<>();

                for (TeachInformation teachInformation : list) {
                    Map<String, Object> stuMap = new LinkedHashMap<>();
                    stuMap.put("id", teachInformation.getId());
                    stuMap.put("username", teachInformation.getTeachId());
                    stuMap.put("name", teachInformation.getTeachName());
                    mapList.add(stuMap);
                }
                return ResultUtil.success(mapList);
            } catch (Exception e) {
                return ResultUtil.error("没有记录");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/allTestSet/{teachId}")
    public ResultVO allTest(
            @PathVariable("teachId") String teachId) {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                return ResultUtil.success(teachService.findTeachDataVO(teachId));
            } catch (Exception e) {
                return ResultUtil.error("没有记录");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }
}
